package com.example.statistics.ui.sales

import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import com.example.statistics.R
import com.example.statistics.bean.CarInfoBean
import com.example.statistics.bean.MarketSales
import com.example.statistics.bean.ProductBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.addEx
import com.example.statistics.ex.format
import com.example.statistics.ex.goTimeFormat
import com.example.statistics.ex.httpPost
import com.example.statistics.net.UrlConfig
import com.example.statistics.ui.car.CarListActivity
import com.example.statistics.ui.product.ProductInfoListActivity
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.example.statistics.ex.toUpperCase
import kotlinx.android.synthetic.main.activity_market_sales_edit.*
import org.jetbrains.anko.toast
import java.util.*

class MarketSalesEditActivity : BaseActivity() {

    private var bean: MarketSales? = null
    private var carId: Int = 0

    override val title: String
        get() = "市场商品流向"


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_market_sales_edit)
        bean = intent.getSerializableExtra(Constants.DATA) as MarketSales?
        if (bean != null) {
            carId = bean?.carId ?: 0
            tv_type.contentText = bean?.attribute
            tv_date.contentText = bean?.date?.goTimeFormat("yyyy-MM-dd HH:mm")
            tv_code.contentText = bean?.factoryNo
            tv_name.contentText = bean?.productName
            tv_number.contentText = bean?.number.toString()
            tv_area.contentText = bean?.salesArea
            tv_car.contentText = bean?.plateNumber?.toUpperCase(Locale.getDefault())
            tv_supplier.contentText = bean?.supplier
            tv_customer.contentText = bean?.customer
        } else {
            tv_date.contentText = Date().format("yyyy-MM-dd HH:mm")
        }
        tv_car.toUpperCase()
        initView()
    }


    private fun initView() {
        var productId: Int? = null

        //类型
        tv_type.onContentClicked {
            tv_type.contentText = when (tv_type.text.toString()) {
                "猪副" -> "牛副"
                "牛副" -> "鸡副"
                "鸡副" -> "猪副"
                else -> ""
            }
        }
        tv_code.onSymbolClicked {
            activityForResult<ProductInfoListActivity>(
                    Constants.ACTION to Constants.SELECTOR,
                    Constants.SEARCH to it
            ) { intent ->
                val data = intent?.getSerializableExtra(Constants.DATA) as ProductBean?
                productId = data?.id
                tv_code.contentText = data?.factoryNo.orEmpty()
                tv_name.contentText = data?.productName.orEmpty()
            }
        }

        //车辆信息
        tv_car.onSymbolClicked {
            activityForResult<CarListActivity>(
                    Constants.ACTION to Constants.SELECTOR,
                    Constants.SEARCH to tv_car.text.toString()
            ) {
                val car = it?.getSerializableExtra(Constants.DATA) as CarInfoBean?
                if (car != null) {
                    carId = car.id
                    val span = SpannableStringBuilder()
                    tv_area.contentText = car.salesArea
                    span.append(car.salesArea)
                    val start = span.length
                    val text = "(${car.plateNumber}+${car.driverPhone})"
                    val end = start.addEx(text.length).toInt()
                    span.append(text).apply {
                        setSpan(AbsoluteSizeSpan(30), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }
                    tv_car.setText(span.toString())
                }
            }
        }

        topBarRightText("保存") {
            val productName = when (tv_name.contentText) {
                "猪手", "前蹄" -> "猪手"
                "猪脚", "后蹄", "猪蹄" -> "猪脚"
                "猪耳", "耳朵", "耳" -> "猪耳"
                "后切", "短切", "短脚" -> "后切"
                "胫骨" -> "颈骨"
                else -> tv_name.contentText
            }
            if (tv_code.isBlank() || tv_name.isBlank() || tv_number.isBlank()) {
                return@topBarRightText
            } else if (carId == 0) {
                toast("请选择车辆")
                return@topBarRightText
            }
            val url = if (bean == null) {
                UrlConfig.MARKET_SALES_ADD
            } else {
                UrlConfig.MARKET_SALES_UPDATE
            }
            httpPost<MarketSales>(url, config = {
                listParams = listOf(
                        "id" to bean?.id,
                        "carId" to carId,
                        "attribute" to tv_type.contentText,
                        "productId" to productId,
                        "factoryNo" to tv_code.contentText,
                        "productName" to productName,
                        "number" to tv_number.contentText,
                        "salesArea" to tv_area.contentText,
                        "supplier" to tv_supplier.contentText,
                        "customer" to tv_customer.contentText,
                        "date" to tv_date.contentText)
            }) {
                activityFinish()
            }
        }
    }
}